package com.hex.ds.hdrs.agent.controller;

import com.hex.ds.hdrs.agent.po.AgentConf;
import com.hex.ds.hdrs.agent.service.RemoteAgentService;
import com.hex.ds.hdrs.agent.vo.AgentServerVo;
import com.hex.ds.ra.common.base.BaseRespInfo;
import com.hex.ds.ra.model.BizAgentModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Package: com.hex.dataService.hdrs.scheduleEngine.controller
 * @ClassName RemoteAgentController
 * @Description: 调度服务操作
 * @Author: Li wenzheng
 * @Date 2021/6/23 17:20
 * @Version v1.0
 **/
@Slf4j
@RestController
@RequestMapping("/api/remoteAgent")
@Api(tags = "应用管理")
public class RemoteAgentController {

    @Autowired
    private RemoteAgentService remoteAgentService;

    /**
     * @param agentConf
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加节点")
    public ResponseEntity<Object> add(@RequestBody AgentConf agentConf) {
        log.info("进入了AgentConfController的add方法");
        this.remoteAgentService.add(agentConf);
        return new ResponseEntity<>("新增成功！", HttpStatus.OK);
    }

    @PostMapping("/saveAndInstall")
    @ApiOperation("保存并发布调度服务")
    public ResponseEntity<Object> saveAndInstall(@RequestBody AgentConf agentConf) {
        log.info("进入了AgentConfController的add方法");
        this.remoteAgentService.saveAndInstall(agentConf);
        return new ResponseEntity<>("发布成功！", HttpStatus.OK);
    }

    @PostMapping("/saveAll")
    @ApiOperation("保存管理AGENT和调度服务")
    public ResponseEntity<Object> saveAll(@RequestBody AgentServerVo agentServerVo) {
        log.info("进入了AgentConfController的add方法");
        this.remoteAgentService.checkName(agentServerVo.getAgentConf());
        this.remoteAgentService.saveAll(agentServerVo);
        return new ResponseEntity<>("新增成功！", HttpStatus.OK);
    }

    @PostMapping("/saveAndInstallAll")
    @ApiOperation("保存并发布管理AGENT和调度服务")
    public ResponseEntity<Object> saveAndInstallAll(@RequestBody AgentServerVo agentServerVo) {
        log.info("进入了AgentConfController的add方法");
        this.remoteAgentService.checkName(agentServerVo.getAgentConf());
        this.remoteAgentService.saveAndInstallAll(agentServerVo);
        return new ResponseEntity<>("发布成功！", HttpStatus.OK);
    }

    /**
     * @Method: install <br>
     * @Param: bizAgentModel <br>
     * @Return: 业务AGENT发布 <br>
     * @Description：<br>
     * @Author： Li wenzheng<br>
     * @Date： 2021/6/16 11:16 <br>
     * @Version： V1.0<br>
     */
    @PostMapping("/install")
    public ResponseEntity<Object> install(@RequestBody BizAgentModel bizAgentModel) {
        log.info("进入【BizAgentController.install】方法");
        BaseRespInfo respInf = this.remoteAgentService.install(bizAgentModel);
        return new ResponseEntity<>(respInf, HttpStatus.OK);
    }

    /**
     * @Method: start <br>
     * @Param: bizAgentModel <br>
     * @Return: 业务AGENT启动 <br>
     * @Description：<br>
     * @Author： Li wenzheng<br>
     * @Date： 2021/6/16 11:16 <br>
     * @Version： V1.0<br>
     */
    @PostMapping("/start")
    public ResponseEntity<Object> start(@RequestBody BizAgentModel bizAgentModel) {
        log.info("进入【BizAgentController.start】方法");
        BaseRespInfo respInf = this.remoteAgentService.start(bizAgentModel);
        return new ResponseEntity<>(respInf, HttpStatus.OK);
    }

    /**
     * @Method: stop <br>
     * @Param: bizAgentModel <br>
     * @Return: 业务AGENT停用 <br>
     * @Description：<br>
     * @Author： Li wenzheng<br>
     * @Date： 2021/6/16 11:16 <br>
     * @Version： V1.0<br>
     */
    @PostMapping("/stop")
    public ResponseEntity<Object> stop(@RequestBody BizAgentModel bizAgentModel) {
        log.info("进入【BizAgentController.stop】方法");
        BaseRespInfo respInf = this.remoteAgentService.stop(bizAgentModel);
        return new ResponseEntity<>(respInf, HttpStatus.OK);
    }

    /**
     * @Method: restart <br>
     * @Param: [bizAgentModel] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：<br>
     * @Author： Li wenzheng<br>
     * @Date： 2021/6/23 14:46 <br>
     * @Version： V1.0<br>
     */
    @PostMapping("/restart")
    public ResponseEntity<Object> restart(@RequestBody BizAgentModel bizAgentModel) {
        log.info("进入【BizAgentController.restart】方法");
        BaseRespInfo respInf = this.remoteAgentService.restart(bizAgentModel);
        return new ResponseEntity<>(respInf, HttpStatus.OK);
    }

    /**
     * @Method: upgrade <br>
     * @Param: [bizAgentModel] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：业务AGENT升级<br>
     * @Author： Li wenzheng<br>
     * @Date： 2021/6/23 14:28 <br>
     * @Version： V1.0<br>
     */
    @PostMapping("/upgrade")
    public ResponseEntity<Object> upgrade(@RequestBody BizAgentModel bizAgentModel) {
        log.info("进入【BizAgentController.upgrade】方法");
        BaseRespInfo respInf = this.remoteAgentService.upgrade(bizAgentModel);
        return new ResponseEntity<>(respInf, HttpStatus.OK);
    }

    /**
     * @Method: restore <br>
     * @Param: [bizAgentModel] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：版本回滚<br>
     * @Author： Li wenzheng<br>
     * @Date： 2021/6/23 14:36 <br>
     * @Version： V1.0<br>
     */
    @PostMapping("/restore")
    public ResponseEntity<Object> restore(@RequestBody BizAgentModel bizAgentModel) {
        log.info("进入【BizAgentController.restore】方法");
        BaseRespInfo respInf = this.remoteAgentService.restore(bizAgentModel);
        return new ResponseEntity<>(respInf, HttpStatus.OK);
    }
}
